/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionmagasins_sans_patterns.forms;

import gestionmagasins_sans_patterns.model.Actions;
import gestionmagasins_sans_patterns.model.Article;
import gestionmagasins_sans_patterns.model.CatalogueArticles;
import gestionmagasins_sans_patterns.model.Taille;
import gestionmagasins_sans_patterns.model.Taxe;
import gestionmagasins_sans_patterns.model.Usager;
import java.awt.AWTEvent;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author James
 */
public class MainMenuJFrame extends CustomJFrame {
    
    private CatalogueArticles articles;
    private ArrayList<Taille> tailles;
    private ArrayList<Taxe> taxes;
    
    public MainMenuJFrame(LoginJFrame loginJFrame, Usager usager) 
    {
        super(loginJFrame, usager);
        
        initComponents();
        this.initKeyListeners();
        
        addComponentListener(componentListener);
        
        taxes = new ArrayList<Taxe>();
        taxes.add(new Taxe("TPS", 0.05f));
        taxes.add(new Taxe("TVQ", 0.095f));
        
        tailles = new ArrayList<Taille>();
        tailles.add(new Taille(1, "p", "Petit"));
        tailles.add(new Taille(2, "m", "Moyen"));
        tailles.add(new Taille(3, "g", "Grand"));
        tailles.add(new Taille(4, "u", "Universel"));
        
        articles = new CatalogueArticles();
        Article newArticle = articles.creerArticle("poiend", "Poire d'endurence", "Poire super énergie qui permet d'augmenter son endurance", tailles.get(3), 4.99, 20);
        newArticle.addTaxe(taxes.get(0));
        newArticle.addTaxe(taxes.get(1));
        articles.addArticle(newArticle);
        
        newArticle = articles.creerArticle("bancou", "Banane de course", "Super banane qui permet courir très vite", tailles.get(3), 3.99, 25);
        newArticle.addTaxe(taxes.get(0));
        articles.addArticle(newArticle);
        
        newArticle = articles.creerArticle("souvolp", "Soulier volant", "Ces souliers vous permet de sauter très haut.", tailles.get(0), 129.99, 4);
        newArticle.addTaxe(taxes.get(0));
        newArticle.addTaxe(taxes.get(1));
        articles.addArticle(newArticle);
        
        newArticle = articles.creerArticle("souvolm", "Soulier volant", "Ces souliers vous permet de sauter très haut.", tailles.get(1), 139.99, 4);
        newArticle.addTaxe(taxes.get(0));
        newArticle.addTaxe(taxes.get(1));
        articles.addArticle(newArticle);
        
        newArticle = articles.creerArticle("souvolg", "Soulier volant", "Ces souliers vous permet de sauter très haut.", tailles.get(2), 149.99, 4);
        newArticle.addTaxe(taxes.get(0));
        newArticle.addTaxe(taxes.get(1));
        articles.addArticle(newArticle);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        manageProductsJButton = new javax.swing.JButton();
        manageClientsJButton = new javax.swing.JButton();
        manageSalesJButton = new javax.swing.JButton();
        manageUsersJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Super Logiciel inc.");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setText("Menu principal");

        manageProductsJButton.setText("Gérer les articles");
        manageProductsJButton.setToolTipText("");
        manageProductsJButton.setName("ManageProducts"); // NOI18N
        manageProductsJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                managementButtonClick(evt);
            }
        });
        manageProductsJButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                managementButtonKeyPressed(evt);
            }
        });

        manageClientsJButton.setText("Gérer les clients");
        manageClientsJButton.setName("ManageClients"); // NOI18N
        manageClientsJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                managementButtonClick(evt);
            }
        });
        manageClientsJButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                managementButtonKeyPressed(evt);
            }
        });

        manageSalesJButton.setText("Gérer les ventes");
        manageSalesJButton.setName("ManageSales"); // NOI18N
        manageSalesJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                managementButtonClick(evt);
            }
        });
        manageSalesJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manageSalesJButtonActionPerformed(evt);
            }
        });
        manageSalesJButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                managementButtonKeyPressed(evt);
            }
        });

        manageUsersJButton.setText("Gérer les usagers");
        manageUsersJButton.setName("ManageUsers"); // NOI18N
        manageUsersJButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                managementButtonClick(evt);
            }
        });
        manageUsersJButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                managementButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(manageProductsJButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(manageSalesJButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(58, 58, 58)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(manageUsersJButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(manageClientsJButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(manageProductsJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(manageClientsJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(manageSalesJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(manageUsersJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void managementButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_managementButtonKeyPressed
        
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            openManagementForm(evt);
    }//GEN-LAST:event_managementButtonKeyPressed

    private void managementButtonClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_managementButtonClick
        openManagementForm(evt);
    }//GEN-LAST:event_managementButtonClick

    private void manageSalesJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageSalesJButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_manageSalesJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton manageClientsJButton;
    private javax.swing.JButton manageProductsJButton;
    private javax.swing.JButton manageSalesJButton;
    private javax.swing.JButton manageUsersJButton;
    // End of variables declaration//GEN-END:variables

    private final ComponentListener componentListener = new ComponentAdapter() 
    {
        @Override
        public void componentShown(ComponentEvent evt) 
        {
          manageUsersJButton.setVisible(usager.getRole().checkRights(Actions.ManageUsers));
        }
    };
        
    private void openManagementForm(AWTEvent senderEvent) 
    {
        
        JFrame managementJFrame = getManagementFormToOpen(getButtonName(senderEvent));
        
        if(managementJFrame != null)
        {
            this.setVisible(false);
            managementJFrame.setVisible(true);
        }
        else
            throw new IllegalArgumentException("Parameter buttonName must not be null!");
    }

    public CatalogueArticles getArticles(){
    
        return articles;
    }
    private String getButtonName(AWTEvent evt) 
    {
        return ((JButton)(evt.getSource())).getName();
    }

    private JFrame getManagementFormToOpen(String buttonName) 
    {
        JFrame managementJFrame = null;
        
        switch(buttonName)
        {
            case "ManageProducts" : 
                managementJFrame = new ManageProductsJFrame(this, this.usager, tailles, taxes, articles);
                break;
                
            case "ManageUsers" : 
                managementJFrame = new ManageUsersJFrame(this, this.usager);
                break;

            case "ManageSales" : 
                managementJFrame = new ManageSalesJFrame(this, this.usager, articles);
                break;

            case "ManageClients" : 
                managementJFrame = new ManageClientsJFrame(this, this.usager);
                break;
                
            default : 
                managementJFrame = null;
                break;
        }
        
        return managementJFrame;
    }
}
